package com.zd.wechatPlatform.controller;

import com.alibaba.fastjson.JSONObject;
import com.zd.wechatPlatform.bean.model.WxAccount;
import com.zd.wechatPlatform.cache.map.WxAccountCache;
import com.zd.wechatPlatform.common.utils.LogUtils;
import com.zd.wechatPlatform.common.utils.WeixinInterfaceUtil;
import com.zd.wechatPlatform.common.utils.XMLUtil;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class RedirectManagerController {
	
	@Autowired
	private WxAccountCache wxAccountCache;

    @RequestMapping("redirectManager")
    public ModelAndView redirectManager(HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            Map<String, String> requestMap = XMLUtil.parseXmlFromInputStream(request.getInputStream());

            LogUtils.debug("requestMap:" + JSONObject.toJSONString(requestMap));
        } catch (Exception e) {
            e.printStackTrace();
        }
        ModelAndView mv = new ModelAndView();
        String code = request.getParameter("code");
        String state = request.getParameter("state");

        String url = request.getParameter("url");
        LogUtils.info("code:" + code + ",state:" + state + ",url:" + url);

        String[] strs = state.split("\\|");
        String appId = strs[0];
        String otherParam = null;
        if (strs.length == 2) {
            otherParam = strs[1];
        }
        LogUtils.debug("otherParam:" + otherParam);
        WxAccount wxAccount = wxAccountCache.getByAppId(appId);
        String responeJson = WeixinInterfaceUtil.getOauthAccessToken(wxAccount.getAppId(), wxAccount.getAppSecret(), code);

        LogUtils.debug("responeJson:" + responeJson);
        JSONObject jsonObject = JSONObject.parseObject(responeJson);
        if ((jsonObject.get("errcode") != null) && (!"".equals(jsonObject.get("errcode")))) {
            LogUtils.error("WeixinInterfaceUtil.getOauthAccessToken fail, errorCode : " + jsonObject.get("errcode") + "  errorMsg : " + jsonObject.get("errmsg"));
        } else {
            String openid = (String) jsonObject.get("openid");
            mv.addObject("openId", openid);
            mv.addObject("appId", appId);
            String accessToken = wxAccount.getAccessToken();
            String jsonWechatUser = WeixinInterfaceUtil.getUserInfo(accessToken, openid, "");
            JSONObject wechatUser = JSONObject.parseObject(jsonWechatUser);
            mv.addObject("img", wechatUser.get("headimgurl"));
            mv.addObject("nickname", wechatUser.get("nickname"));
            LogUtils.debug("jsonWechatUser:" + jsonWechatUser);
        }
        mv.setViewName("redirect:" + url);
        LogUtils.debug("jsonWechatUser:" + url);
        return mv;
    }
    
    
    @RequestMapping("redirectManagerOnlyOpenid")
    public ModelAndView redirectManagerOnlyOpenid(HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            Map<String, String> requestMap = XMLUtil.parseXmlFromInputStream(request.getInputStream());

            LogUtils.debug("requestMap:" + JSONObject.toJSONString(requestMap));
        } catch (Exception e) {
            e.printStackTrace();
        }
        ModelAndView mv = new ModelAndView();
        String code = request.getParameter("code");
        String state = request.getParameter("state");

        String url = request.getParameter("url");
        LogUtils.info("code:" + code + ",state:" + state + ",url:" + url);

        String[] strs = state.split("\\|");
        String appId = strs[0];
        String otherParam = null;
        if (strs.length == 2) {
            otherParam = strs[1];
        }
        LogUtils.debug("otherParam:" + otherParam);
        WxAccount wxAccount = wxAccountCache.getByAppId(appId);
        String responeJson = WeixinInterfaceUtil.getOauthAccessToken(wxAccount.getAppId(), wxAccount.getAppSecret(), code);

        LogUtils.debug("responeJson:" + responeJson);
        JSONObject jsonObject = JSONObject.parseObject(responeJson);
        if ((jsonObject.get("errcode") != null) && (!"".equals(jsonObject.get("errcode")))) {
            LogUtils.error("WeixinInterfaceUtil.getOauthAccessToken fail, errorCode : " + jsonObject.get("errcode") + "  errorMsg : " + jsonObject.get("errmsg"));
        } else {
            String openid = (String) jsonObject.get("openid");
            mv.addObject("openId", openid);
            mv.addObject("appId", appId);
        }
        mv.setViewName("redirect:" + url);
        LogUtils.debug("jsonWechatUser:" + url);
        return mv;
    }
}
